package cn.xluobo.business.wechat.miniapp.course.service;

import cn.xluobo.business.sc.course.domain.req.ReqSearchScCourseCla;
import cn.xluobo.business.sc.course.domain.resp.RespCourseClaInfo;
import cn.xluobo.business.sc.course.repo.mapper.ScCourseClaMapper;
import cn.xluobo.business.sys.admin.enums.RoleEnum;
import cn.xluobo.business.sys.admin.service.ISysUserService;
import cn.xluobo.config.login.LoginUser;
import cn.xluobo.core.api.APIResponse;
import cn.xluobo.core.page.RespPage;
import cn.xluobo.utils.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：KingLong
 * @date ：Created in 2020/11/3 09:17
 */
@Service
@Transactional
public class WechatMiniClaService {

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ScCourseClaMapper scCourseClaMapper;

    /**
     * 班级列表
     * 校长/教务管理 可查询当前校区所有班级
     * 教师 可查询自己教课的班级
     * @param reqSearchScCourseCla
     * @return
     */
    public APIResponse searchList(ReqSearchScCourseCla reqSearchScCourseCla) {
        LoginUser loginUser = LoginUserUtil.getLoginUser();
        Long deptId = loginUser.getDeptId();

        // 是否拥有管理角色
        boolean allSchoolData = userService.checkUserHasAnyRole(loginUser.getUserId(), loginUser.getNowTenantId(), new String[]{
                RoleEnum.SCHOOL_MANAGER.getRoleCode(),
                RoleEnum.TEACHING_MANAGER.getRoleCode()
        });

        if(!allSchoolData) {
            reqSearchScCourseCla.setStaffId(loginUser.getStaffId());
        }
        // 当前校区
        reqSearchScCourseCla.setDeptId(deptId);
        // 用户拥有校区
        reqSearchScCourseCla.setUserId(loginUser.getUserId());

        RespPage<RespCourseClaInfo> page = new RespPage<>(reqSearchScCourseCla.getPageNum(), reqSearchScCourseCla.getPageSize());
        List<RespCourseClaInfo> claList = scCourseClaMapper.selectClaList(reqSearchScCourseCla, page);
        page.setRows(claList);
        return APIResponse.toAPIResponse(page);
    }

}
